using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DigitalTrainSuite.Canvas;

namespace DigitalTrainSuite.UserControls
{
	public delegate bool ControlSwitch(int switchID, bool switchPosition);
	

	public partial class UserControlTurnoutController : UserControl, IMoveableUserControl, IDeleteable
	{

	#region public events

		public event ControlSwitch controlSwitch;
		public event DeleteControl deleteControl;

		#endregion

	#region private members

		private int switchID = -1;
		private bool moving = false;
		private Point origin;
		private bool enableControl = true;
		private bool movable = true;
		private TurnoutPosition position = TurnoutPosition.Straight;
		private RotateFlipType rotation = RotateFlipType.RotateNoneFlipNone;
		private ContextMenuStrip rightClickMenu;

		#endregion

	#region public methods

		public UserControlTurnoutController()
		{
			InitializeComponent();
		}

		public UserControlTurnoutController(int switchID, Point location, ControlSwitch controlSwitch)
		{
			InitializeComponent();
			this.SwitchID = switchID;
			this.Location = location;
			this.controlSwitch += controlSwitch;
			this.EnableControl = false;
		}

		public UserControlTurnoutController(int switchID, Point location, ControlSwitch controlSwitch, RotateFlipType rotation)
		{
			InitializeComponent();
			this.SwitchID = switchID;
			this.Location = location;
			this.controlSwitch += controlSwitch;
			this.Rotation = rotation;
			this.EnableControl = false;
		}

		public UserControlTurnoutController(int switchID, Point location, ControlSwitch controlSwitch, RotateFlipType rotation, TurnoutPosition initialPosition)
		{
			InitializeComponent();
			this.SwitchID = switchID;
			this.Location = location;
			this.controlSwitch += controlSwitch;
			this.Rotation = rotation;
			this.Position = initialPosition;
			this.EnableControl = false;
		}

		public UserControlTurnoutController(int switchID, Point location)
		{
			InitializeComponent();
			this.SwitchID = switchID;
			this.Location = location;
			this.EnableControl = false;
		}

		public UserControlTurnoutController(int switchID, Point location, RotateFlipType rotation)
		{
			InitializeComponent();
			this.SwitchID = switchID;
			this.Location = location;
			this.Rotation = rotation;
			this.EnableControl = false;
		}

		public UserControlTurnoutController(int switchID, Point location, RotateFlipType rotation, TurnoutPosition initialPosition)
		{
			InitializeComponent();
			this.SwitchID = switchID;
			this.Location = location;
			this.Rotation = rotation;
			this.Position = initialPosition;
			this.EnableControl = false;
		} 

		#endregion

	#region public properties

		public int SwitchID
		{
			get
			{
				return this.switchID;
			}
			set
			{
				this.switchID = value;
				this.labelSwitchID.Text = value.ToString();
			}
		}

		public bool EnableControl
		{
			get { return this.enableControl; }
			set
			{
				this.radioButtonCurve.Enabled = value;
				this.radioButtonStraight.Enabled = value;
			}
		}

		public bool Movable
		{
			get { return this.movable; }
			set
			{
				this.movable = value;
				if (!value)
				{
					this.ContextMenuStrip = null;
				}
				else
				{
					if (this.Mode == CanvasMode.Controller) this.RightClickMenu = this.contextMenuStripRotate;
					else this.RightClickMenu = this.contextMenuStripEdit;
				}
			}
		}

		public ContextMenuStrip RightClickMenu
		{
			get { return this.rightClickMenu; }
			set
			{
				this.rightClickMenu = value;
				this.ContextMenuStrip = value;
				this.labelSwitchID.ContextMenuStrip = value;
				this.radioButtonCurve.ContextMenuStrip = value;
				this.radioButtonStraight.ContextMenuStrip = value;
			}
		}

		public TurnoutPosition Position
		{
			get { return this.position; }
			private set
			{
				this.position = value;
				if (value == TurnoutPosition.Green)
				{
					this.radioButtonCurve.Checked = false;
					this.radioButtonStraight.Checked = true;
				}
				else
				{
					this.radioButtonCurve.Checked = true;
					this.radioButtonStraight.Checked = false;
				}
			}
		}

		public bool AddressVisible
		{
			get { return this.labelSwitchID.Visible; }
			set { this.labelSwitchID.Visible = value; }
		}

		public RotateFlipType Rotation
		{
			get { return this.rotation; }
			set
			{
				if (this.movable)
				{
					switch (value)
					{
						case RotateFlipType.Rotate180FlipNone:
						case RotateFlipType.Rotate180FlipX:
							this.radioButtonCurve.Location = new Point(2, 2);
							this.radioButtonStraight.Location = new Point(2, 16);
							this.labelSwitchID.Location = new Point(16, 7);
							this.Size = new Size(32, 30);
							this.rotation = RotateFlipType.Rotate180FlipNone;
							break;
						case RotateFlipType.Rotate270FlipNone:
						case RotateFlipType.Rotate270FlipY:
							this.radioButtonCurve.Location = new Point(16, 2);
							this.radioButtonStraight.Location = new Point(2, 2);
							this.labelSwitchID.Location = new Point(32, 2);
							this.Size = new Size(50, 16);
							this.rotation = RotateFlipType.Rotate270FlipNone;
							break;
						case RotateFlipType.Rotate90FlipNone:
						case RotateFlipType.Rotate90FlipY:
							this.radioButtonCurve.Location = new Point(2, 2);
							this.radioButtonStraight.Location = new Point(16, 2);
							this.labelSwitchID.Location = new Point(32, 2);
							this.Size = new Size(50, 16);
							this.rotation = RotateFlipType.Rotate90FlipNone;
							break;
						default:
							this.radioButtonCurve.Location = new Point(2, 16);
							this.radioButtonStraight.Location = new Point(2, 2);
							this.labelSwitchID.Location = new Point(16, 7);
							this.Size = new Size(32, 30);
							this.rotation = RotateFlipType.RotateNoneFlipNone;
							break;
					}
				}
			}
		}

		private CanvasMode mode = CanvasMode.Controller;

		public CanvasMode Mode
		{
			get { return mode; }
			set 
			{ 
				mode = value;
				switch (value)
				{
					case CanvasMode.Controller:
						this.RightClickMenu = this.contextMenuStripRotate;
						break;
					default:
						this.RightClickMenu = this.contextMenuStripEdit;
						break;
				}
			}
		}
	

		#endregion

	#region private event handlers

		private void noRotationToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Rotation = RotateFlipType.RotateNoneFlipNone;
		}

		private void rotate90ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Rotation = RotateFlipType.Rotate90FlipNone;
		}

		private void rotate180ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Rotation = RotateFlipType.Rotate180FlipNone;
		}

		private void rotate270ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Rotation = RotateFlipType.Rotate270FlipNone;
		}


		private void radioButtonStraight_Clicked(object sender, EventArgs e)
		{
			if (this.switchID == -1 || this.controlSwitch == null) return;

			this.radioButtonCurve.Checked = false;
			this.radioButtonStraight.Checked = true;
			if (this.controlSwitch != null) this.controlSwitch(this.switchID, true);
			this.position = TurnoutPosition.Straight;
		}

		private void radioButtonCurve_Clicked(object sender, EventArgs e)
		{
			if (this.switchID == -1 || this.controlSwitch == null) return;

			this.radioButtonCurve.Checked = true;
			this.radioButtonStraight.Checked = false;
			if (this.controlSwitch != null) this.controlSwitch(this.switchID, false);
			this.position = TurnoutPosition.Curve;
		}

		private void UserControlTurnoutController_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.moving)
			{
				this.Location = new Point(this.Location.X + e.X - this.origin.X, this.Location.Y + e.Y - this.origin.Y);
			}
		}

		private void UserControlTurnoutController_MouseDown(object sender, MouseEventArgs e)
		{
			if (this.movable)
			{
				this.origin = new Point(e.X, e.Y);
				this.moving = true;
			}
		}

		private void UserControlTurnoutController_MouseUp(object sender, MouseEventArgs e)
		{
			this.moving = false;
		} 

		#endregion

		private void toolStripTextBoxID_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				Convert.ToInt32(this.toolStripTextBoxID.Text);
			}
			catch
			{
				this.toolStripTextBoxID.Text = "";
			}
		}

		private void toolStripTextBoxID_TextChanged(object sender, EventArgs e)
		{
			try
			{
				this.SwitchID = Convert.ToInt32(this.toolStripTextBoxID.Text);
			}
			catch
			{
				this.toolStripTextBoxID.Text = "";
			}
		}

		private void toolStripComboBoxRotation_SelectedIndexChanged(object sender, EventArgs e)
		{
			switch (this.toolStripComboBoxRotation.SelectedIndex)
			{
				case 0:
					this.Rotation = RotateFlipType.RotateNoneFlipNone;
					break;
				case 1:
					this.Rotation = RotateFlipType.Rotate90FlipNone;
					break;
				case 2:
					this.Rotation = RotateFlipType.Rotate180FlipNone;
					break;
				case 3:
					this.Rotation = RotateFlipType.Rotate270FlipNone;
					break;
				default:
					break;
			}
		}

		private void contextMenuStripEdit_Opening(object sender, CancelEventArgs e)
		{
			this.toolStripTextBoxID.Text = this.switchID.ToString();
			this.toolStripComboBoxRotation.Text = this.Rotation.ToString();
		}

		private void toolStripTextBoxID_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				try
				{
					this.SwitchID = Convert.ToInt32(this.toolStripTextBoxID.Text);
					this.contextMenuStripEdit.Close();
				}
				catch
				{
					this.toolStripTextBoxID.Text = "";
				}
			}
		}

		private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.deleteControl != null)
			{
				this.deleteControl(this);
			}
		}

		#region IDeleteable Members


		public void deleteThis()
		{
			if (this.deleteControl != null)
			{
				this.deleteControl(this);
			}
		}

		#endregion
	}
}
